/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.openbookformat.project.newprojectwizard;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListModel;
import org.openide.util.NbBundle;

public final class NewOBFProjectVisualPanel2 extends JPanel {

    public static final String PROP_SUBPAGES = "subpages";
    public static final String PROP_HEADINGFROMSUBPAGES = "headingFromSubpages";
    public static final String PROP_NUMBEROFCHAPTERS = "numberOfChapters";
    
    /** Creates new form NewOBFProjectVisualPanel2 */
    public NewOBFProjectVisualPanel2() {
        initComponents();
    }

    @Override
    public String getName() {
        return NbBundle.getMessage(NewOBFProjectVisualPanel2.class, 
                "NewOBFProjectVisualPanel2.Name");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        subPagesList = new javax.swing.JList();
        jHeadingFromSubpages = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        jNumberOfChpt = new javax.swing.JSpinner();
        jAdd = new javax.swing.JButton();
        jDel = new javax.swing.JButton();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel2.class, "NewOBFProjectVisualPanel2.jLabel1.text")); // NOI18N

        jScrollPane1.setViewportView(subPagesList);

        jHeadingFromSubpages.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(jHeadingFromSubpages, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel2.class, "NewOBFProjectVisualPanel2.jHeadingFromSubpages.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel2.class, "NewOBFProjectVisualPanel2.jLabel2.text")); // NOI18N

        jNumberOfChpt.setModel(new javax.swing.SpinnerNumberModel(5, 1, 100, 1));

        org.openide.awt.Mnemonics.setLocalizedText(jAdd, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel2.class, "NewOBFProjectVisualPanel2.jAdd.text")); // NOI18N
        jAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAddActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jDel, org.openide.util.NbBundle.getMessage(NewOBFProjectVisualPanel2.class, "NewOBFProjectVisualPanel2.jDel.text")); // NOI18N
        jDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jHeadingFromSubpages)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDel))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                    .addComponent(jNumberOfChpt, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jAdd)
                    .addComponent(jDel))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jNumberOfChpt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jHeadingFromSubpages)
                .addContainerGap(69, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAddActionPerformed
        int sIdx = subPagesList.getSelectedIndex();
        if(sIdx == -1)
            sIdx = subPagesList.getModel().getSize() -1;
        String lk = (String) subPagesList.getModel().getElementAt(sIdx);
        
        String link = JOptionPane.showInputDialog("Add link...", lk);
        ((DefaultListModel) subPagesList.getModel()).insertElementAt(link, sIdx+1);
    }//GEN-LAST:event_jAddActionPerformed

    private void jDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDelActionPerformed
        int[] sIdx = subPagesList.getSelectedIndices();
        if(sIdx.length == 0)
            return;
        for(int i=0; i < sIdx.length; i++)
            ((DefaultListModel) subPagesList.getModel()).remove(sIdx[i]);
    }//GEN-LAST:event_jDelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAdd;
    private javax.swing.JButton jDel;
    private javax.swing.JCheckBox jHeadingFromSubpages;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSpinner jNumberOfChpt;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList subPagesList;
    // End of variables declaration//GEN-END:variables
    
    
    public void setSubPagesModel(ListModel model) {
        subPagesList.setModel(model);
    }
    
    
    /**
     * Get the value of subpages
     *
     * @return the value of subpages
     */
    public ArrayList<String> getSubpages() {
        ArrayList<String> r = new ArrayList<String>();
        Object[] sValues = subPagesList.getSelectedValues();
        for(int i =0; i< sValues.length; i++)
            r.add((String) sValues[i]);
        return r;
    }
    
    /**
     * Get the value of numberOfChapters
     *
     * @return the value of numberOfChapters
     */
    public int getNumberOfChapters() {
        return ((Integer) jNumberOfChpt.getValue()).intValue();
    }

    /**
     * Get the value of headingFromSubpages
     *
     * @return the value of headingFromSubpages
     */
    public boolean isHeadingFromSubpages() {
        return jHeadingFromSubpages.isSelected();
    }
    
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

}
